package com.wechat.servlet.FriendRequest;

import com.alibaba.fastjson.JSONArray;
import com.wechat.DAO.DaoImpl.FriendRequestDAOIpml;
import com.wechat.DAO.DaoImpl.UserDAOIpml;
import com.wechat.DAO.DaoImpl.UserInfoDAOIpml;
import com.wechat.DAO.DaoImpl.UserRelationDAOIpml;
import com.wechat.DAO.FriendRequestDAO;
import com.wechat.DAO.UserDAO;
import com.wechat.DAO.UserInfoDAO;
import com.wechat.DAO.UserRelationDAO;
import com.wechat.pojo.FriendRequest;
import com.wechat.pojo.User;
import com.wechat.pojo.UserInfo;
import com.wechat.pojo.UserRelation;
import com.wechat.servlet.Utils.Base64Encoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FriendResponseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String SID = request.getParameter("SID");
        String RID = request.getParameter("RID");
        String result = request.getParameter("result");
        FriendRequestDAO friendRequestDAO = new FriendRequestDAOIpml();
        UserRelationDAO userRelationDAO = new UserRelationDAOIpml();
        try {
            friendRequestDAO.updateStatus(SID,RID,result);
            if(request.equals("yes")){
                userRelationDAO.addRelation(SID,RID,1);
                userRelationDAO.addRelation(RID,SID,1);
            }else if(request.equals("no")){
                //这里根据具体需求更改，如果不通知好友请求发送端就直接删除，否则保留
                friendRequestDAO.delete(SID,RID);
            }
        }catch (Exception e){
            response.getWriter().write("fail");
        }
        response.getWriter().write("success");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String SID = request.getParameter("SID");
        FriendRequestDAO friendRequestDAO = new FriendRequestDAOIpml();
//        List<FriendRequest> okFriendRequests = new ArrayList<>();
        UserInfoDAO userInfoDAO = new UserInfoDAOIpml();
        List<UserInfo> userInfos = new ArrayList<>();
        try {
            List<FriendRequest> friendRequests = friendRequestDAO.queryBySID(SID);
            if(friendRequests == null || friendRequests.isEmpty()){
                response.getWriter().write("null");
                return;
            }
            for(FriendRequest friendRequest: friendRequests){
                if(friendRequest.getStatus().equals("yes")){
                    UserInfo userInfo = userInfoDAO.query(friendRequest.getRID());
                    userInfos.add(userInfo);
                }
            }
            if(userInfos!=null){
                JSONArray jsonArray = (JSONArray) JSONArray.toJSON(userInfos);
                String base64 = Base64Encoder.encode(jsonArray.toString());
                response.getWriter().write(base64);
            }else {
                response.getWriter().write("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("fail");
        }
    }
}
